﻿
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core.XREF.Newtonsoft.Json;

namespace Updater.Core
{
    /// <summary>
    /// 快速Json.类只要继承了FastJson即可拥有序列化与反序列化的能力及将类保存到文件的能力
    /// </summary>
    public abstract class FastJson
   {
       /// <summary>
       /// 将XML转换为OBject
       /// </summary>
       /// <param name="configFilePath"></param>
       /// <returns></returns>
        public static T Load<T>(string configFilePath)
        {
            string str= File.ReadAllText(configFilePath);



            return JsonConvert.DeserializeObject<T>(str);



        }
      /// <summary>
      /// 将配置序列化到文件
      /// </summary>
      /// <param name="configFilePath"></param>
        public void Save(string configFilePath)
        {
           //序列化到字符串
           string str= JsonConvert.SerializeObject(this);
           //写入配置文件
           File.WriteAllText(configFilePath, str);

        }
        /// <summary>
        /// 将本身序列化为Byte
        /// </summary>
        /// <returns></returns>
        public byte[] SerializObjectToByte()
        {
            return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(this));
        }
        /// <summary>
        /// 将任何类型序列化成byte
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static byte[] SerializObjectToByte<T>(T t)
        {
            return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(t));
        }
        /// <summary>
        /// 反序列化-静态-将文本反序列化成指定类型
        /// </summary>
        /// <typeparam name="T">自指定类型</typeparam>
        /// <param name="str">序列化文本数据</param>
        /// <returns></returns>
        public static T DeserializeObject<T>(string str)
        {
            return JsonConvert.DeserializeObject<T>(str);
        }
        /// <summary>
        /// 反序列化-静态-将byte[]-反序列化成指定类型
        /// </summary>
        /// <typeparam name="T">自指定类型</typeparam>
        /// <param name="Bytes">反序列化Bytes数据</param>
        /// <returns></returns>
        public static T DeserializeObject<T>(byte[] Bytes)
        {
            return JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(Bytes));
        }


    }
}
